home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / terminal / qterm-6.0 / qterm-6 / aux.c next >
Encoding:
C/C++ Source or Header  |  1993-07-12  |  4.4 KB  |  258 lines

  1. #ifndef lint
  2. static char RCSid[] = 
  3. "$Id: aux.c,v 6.4 1993/07/12 20:15:23 mcooper Exp mcooper $";
  4.  
  5. static char copyright[] =
  6. "@(#) Copyright (c) 1990-1993 Michael A. Cooper.\n\
  7.  All rights reserved.\n";
  8. #endif
  9.  
  10. /*
  11.  * Copyright (c) 1990-1993 Michael A. Cooper.
  12.  * This software may be freely distributed provided it is not sold for 
  13.  * profit and the author is credited appropriately.
  14.  */
  15.  
  16. #include "config.h"
  17. #include "qterm.h"
  18. #include <stdio.h>
  19. #include <errno.h>
  20.  
  21. /*
  22.  * Get system error string
  23.  */
  24. char *SysErr()
  25. {
  26.     extern int            errno;
  27.     extern int            sys_nerr;
  28.     extern char               *sys_errlist[];
  29.     static char            buff[BUFSIZ];
  30.  
  31.     if (errno > 0 && errno < sys_nerr)
  32.     return(sys_errlist[errno]);
  33.     else {
  34.     (void) sprintf(buff, "Unknown error %d", errno);
  35.     return(buff);
  36.     }
  37. }
  38.  
  39. #if     ARG_TYPE == ARG_VARARGS
  40. /*
  41.  * Varargs Error()
  42.  */
  43. void Error(va_alist)
  44.     va_dcl
  45. {
  46.     va_list             args;
  47.     char               *fmt;
  48.  
  49.     (void) fprintf(stderr, "%s: ", ProgName);
  50.  
  51.     va_start(args);
  52.     fmt = va_arg(args, char *);
  53.     (void) vfprintf(stderr, fmt, args);
  54.     va_end(args);
  55.  
  56.     (void) fprintf(stderr, "\r\n");
  57. }
  58. #endif    /* ARG_VARARGS */
  59.  
  60. #if     ARG_TYPE == ARG_STDARGS
  61. /*
  62.  * Stdargs Error()
  63.  */
  64. void Error(char *fmt, ...)
  65. {
  66.     va_list             args;
  67.  
  68.     (void) fprintf(stderr, "%s: ", ProgName);
  69.  
  70.     va_start(args, fmt);
  71.     if (fmt)
  72.     (void) vfprintf(stderr, fmt, args);
  73.     va_end(args);
  74.  
  75.     (void) fprintf(stderr, "\r\n");
  76. }
  77. #endif    /* ARG_VARARGS */
  78.  
  79. #if     !defined(ARG_TYPE)
  80. /*
  81.  * Simple Error()
  82.  */
  83. void Error(fmt, a1, a2, a3, a4, a5, a6)
  84.     char *fmt;
  85. {
  86.     (void) fprintf(stderr, "%s: ", ProgName);
  87.  
  88.     if (fmt)
  89.     (void) fprintf(stderr, fmt, a1, a2, a3, a4, a5, a6);
  90.  
  91.     (void) fprintf(stderr, "\r\n");
  92. }
  93. #endif    /* ARG_VARARGS */
  94.  
  95.  
  96. #if     ARG_TYPE == ARG_VARARGS
  97. /*
  98.  * Varargs dprintf()
  99.  */
  100. void dprintf(va_alist)
  101.     va_dcl
  102. {
  103.     va_list             args;
  104.     char               *fmt;
  105.  
  106.     if (!Debug)
  107.     return;
  108.  
  109.     va_start(args);
  110.     fmt = va_arg(args, char *);
  111.     (void) vfprintf(stderr, fmt, args);
  112.     va_end(args);
  113. }
  114. #endif    /* ARG_VARARGS */
  115.  
  116. #if     ARG_TYPE == ARG_STDARGS
  117. /*
  118.  * Stdargs dprintf()
  119.  */
  120. void dprintf(char *fmt, ...)
  121. {
  122.     va_list             args;
  123.  
  124.     if (!Debug)
  125.     return;
  126.  
  127.     va_start(args, fmt);
  128.     if (fmt)
  129.     (void) vfprintf(stderr, fmt, args);
  130.     va_end(args);
  131. }
  132. #endif    /* ARG_VARARGS */
  133.  
  134. #if     !defined(ARG_TYPE)
  135. /*
  136.  * Simple dprintf()
  137.  */
  138. void dprintf(fmt, a1, a2, a3, a4, a5, a6)
  139.     char *fmt;
  140. {
  141.     if (!Debug)
  142.     return;
  143.  
  144.     if (fmt)
  145.     (void) fprintf(stderr, fmt, a1, a2, a3, a4, a5, a6);
  146. }
  147. #endif    /* ARG_VARARGS */
  148.  
  149.  
  150. /*
  151.  * xmalloc - Do a malloc with error checking.
  152.  */
  153. char *xmalloc(size)
  154.     int             size;
  155. {
  156.     char                *p;
  157.     char               *malloc();
  158.     
  159.     if ((p = malloc((unsigned) size)) == NULL) {
  160.     Error("malloc %d bytes failed: %s.", size, SYSERR);
  161.     Done(1);
  162.     /*NOTREACHED*/
  163.     }
  164.     
  165.     return(p);
  166. }
  167.  
  168. #if    RE_TYPE == RE_BSD
  169. /*
  170.  * BSD style regular expression matching
  171.  */
  172. int RegExMatch(re, string)
  173.     char               *re;
  174.     char               *string;
  175. {
  176.     char               *errmsg;
  177.     char               *re_comp();
  178.  
  179.     if (errmsg = re_comp(re)) {
  180.     Error("Bad regular expression: \"%s\": %s.", re, errmsg);
  181.     return(-1);
  182.     }
  183.  
  184.     if (re_exec(string) == 1)
  185.     return(TRUE);    /* Matched */
  186.  
  187.     return(0);
  188. }
  189. #endif    /* RE_BSD */
  190.  
  191. #if    RE_TYPE == RE_SYSV
  192. /*
  193.  * SYSV style regular expression matching
  194.  */
  195. int RegExMatch(re, string)
  196.     char               *re;
  197.     char               *string;
  198. {
  199.     char               *reptr;
  200.     char               *regcmp();
  201.     int                status = 0;
  202.  
  203.     if ((reptr = regcmp(re, (char *)NULL)) == (char *)NULL) {
  204.     Error("Bad regular expression: \"%s\".", re);
  205.     return(-1);
  206.     }
  207.  
  208.     if (regex(reptr, string))
  209.     status = 1;    /* Matched */
  210.  
  211.     (void) free(reptr);
  212.     return(status);
  213. }
  214. #endif    /* RE_SYSV */
  215.  
  216. #if    RE_TYPE == RE_REGCOMP
  217. /*
  218.  * HP/UX style regular expression matching
  219.  */
  220. #include <regex.h>
  221.  
  222. int RegExMatch(pattern, string)
  223.     char               *pattern;
  224.     char               *string;
  225. {
  226.     int                status = 0;
  227.     regex_t            re;
  228.  
  229.     if (regcomp(&re, pattern, REG_NOSUB|REG_NEWLINE)) {
  230.     Error("Bad regular expression: \"%s\".", pattern);
  231.     return(-1);
  232.     }
  233.  
  234.     if (regex(&re, string, (size_t) 0, NULL, 0) == 0)
  235.     status = 1;    /* Matched */
  236.  
  237.     regfree(&re);
  238.     return(status);
  239. }
  240. #endif    /* RE_REGCOMP */
  241.  
  242. /*
  243.  * Copy a string
  244.  */
  245. char *StrCopy(ptr)
  246.     char               *ptr;
  247. {
  248.     char               *newptr;
  249.  
  250.     if (!ptr)
  251.     return((char *)NULL);
  252.  
  253.     newptr = xmalloc(strlen(ptr)+1);
  254.     (void) strcpy(newptr, ptr);
  255.  
  256.     return(newptr);
  257. }
  258.